{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "aspose html转pdf_pdfkit | 利用python实现html文件转pdf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "options = {\n",
    "    '--enable-local-file-access': '--enable-local-file-access',\n",
    "    '--header-html': 'http://192.168.168.126:8185/report/header.html', # 该url获取到仅包含logo图片的html页面\n",
    "    '--header-right': '页眉右侧文字',\n",
    "    '--header-line': '--header-line', # 页眉下划线\n",
    "    '--header-spacing': 5, # 页眉距离内容高度\n",
    "    'page-size': 'Letter',\n",
    "    'margin-top': '0.75in',\n",
    "    'margin-right': '0.75in',\n",
    "    'margin-bottom': '0.75in',\n",
    "    'margin-left': '0.75in',\n",
    "    'encoding': \"UTF-8\",  #支持中文\n",
    "    '--footer-spacing': 10,\n",
    "    '--footer-html': 'http://192.168.168.126:8185/report/footer.html'\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "完成\n"
     ]
    }
   ],
   "source": [
    "import pdfkit\n",
    "\n",
    "def url_to_pdf(url,to_file):\n",
    "    path_wkthmltopdf = r'D:\\wkhtmltox\\bin\\wkhtmltopdf.exe'\n",
    "    config = pdfkit.configuration(wkhtmltopdf=path_wkthmltopdf)   \n",
    "    pdfkit.from_url(url,to_file,configuration=config,options=options)\n",
    "    print(\"完成\")\n",
    "\n",
    "url_to_pdf('http://192.168.168.126:8185/report/a.html','out.pdf')\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "from PyPDF2 import PdfFileReader, PdfFileWriter\n",
    "from reportlab.lib.units import cm\n",
    "from reportlab.pdfgen import canvas\n",
    "\n",
    "\n",
    "def create_watermark(content):\n",
    "    \"\"\"水印信息\"\"\"\n",
    "    # 默认大小为21cm*29.7cm\n",
    "    file_name = \"mark.pdf\"\n",
    "    c = canvas.Canvas(file_name, pagesize=(21 * cm, 29.7 * cm))\n",
    "    # 移动坐标原点(坐标系左下为(0,0))\n",
    "    c.translate(5 * cm, 10 * cm)\n",
    "\n",
    "    # 设置字体\n",
    "    c.setFont(\"Helvetica\", 80)\n",
    "    # 指定描边的颜色\n",
    "    c.setStrokeColorRGB(0, 1, 0)\n",
    "    # 指定填充颜色\n",
    "    c.setFillColorRGB(0, 1, 0)\n",
    "    # 画一个矩形\n",
    "    # c.rect(cm, cm, 7*cm, 17*cm, fill=1)\n",
    "    # 旋转45度,坐标系被旋转\n",
    "    c.rotate(30)\n",
    "    # 指定填充颜色\n",
    "    c.setFillColorRGB(0, 0, 0, 0.1)\n",
    "    # 设置透明度,1为不透明\n",
    "    # c.setFillAlpha(0.1)\n",
    "    # 画几个文本,注意坐标系旋转的影响\n",
    "    c.drawString(3 * cm, 0 * cm, content)\n",
    "    c.setFillAlpha(0.6)\n",
    "    # 关闭并保存pdf文件\n",
    "    c.save()\n",
    "    return file_name\n",
    "\n",
    "def add_watermark(pdf_file_in, pdf_file_mark, pdf_file_out):\n",
    "    \"\"\"把水印添加到pdf中\"\"\"\n",
    "    pdf_output = PdfFileWriter()\n",
    "    input_stream = open(pdf_file_in, 'rb')\n",
    "    pdf_input = PdfFileReader(input_stream, strict=False)\n",
    "\n",
    "    # 获取PDF文件的页数\n",
    "    pageNum = pdf_input.getNumPages()\n",
    "\n",
    "    # 读入水印pdf文件\n",
    "    pdf_watermark = PdfFileReader(open(pdf_file_mark, 'rb'), strict=False)\n",
    "    # 给每一页打水印\n",
    "    for i in range(pageNum):\n",
    "        \n",
    "        page = pdf_input.getPage(i)\n",
    "        page.mergePage(pdf_watermark.getPage(0))\n",
    "        page.compressContentStreams()  # 压缩内容\n",
    "        pdf_output.addPage(page)\n",
    "    pdf_output.write(open(pdf_file_out, 'wb'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "pdf_file_in = 'out.pdf'\n",
    "pdf_file_out = 'watermark.pdf'\n",
    "pdf_file_mark = create_watermark('爱升科技')\n",
    "add_watermark(pdf_file_in, pdf_file_mark, pdf_file_out)"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "73e03da126b73bfff3642ec5261d56fa25c444ea595de51041687efaa60dda41"
  },
  "kernelspec": {
   "display_name": "Python 3.8.8 64-bit ('base': conda)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
